package com.hopedove.yieldmanage.adapter;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.hopedove.utils.ToastUtil;
import com.hopedove.view.SimpleDialog;
import com.hopedove.view.SimpleInputDialog;
import com.hopedove.yieldmanage.R;
import com.hopedove.yieldmanage.entity.ProductInputEntity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class InputListAdapter extends BaseAdapter {
    private Context mContext;

    /**
     * 存储原因下拉框文本
     **/
    private List<String> mReasonSpList = new ArrayList<String>();

    /**
     * 存储班次下拉框文本
     **/
    private List<String> mClassesSpList = new ArrayList<String>();
    /**
     * 下拉框适配器
     **/
    ArrayAdapter<String> mReasonSpinnerAdapter;

    ArrayAdapter<String> mClassSpinnerAdapter;

    private List<ProductInputEntity> mProductList;

    private ListView mListView;

    private boolean isEditBtnClicked = false;
    /**
     * 记录点击的下标
     **/
    private int mIndex = -1;
    /**
     * 记录点击的 edittext
     **/
    private EditText mEditText;

    private boolean isFirst;

    public InputListAdapter(Context context, List<ProductInputEntity> objects, String jobDate) {
        super();
        mContext = context;
        // 这里集合用=号赋值，改变内部productlist 外部list一样会变
        mProductList = objects;
        mClassesSpList = Arrays.asList(mContext.getResources().getStringArray(R.array.classes));
        mReasonSpinnerAdapter = new ArrayAdapter<>(mContext, R.layout.yield_spinner_item, mReasonSpList);
        mReasonSpinnerAdapter.setDropDownViewResource(R.layout.yield_spinner_drop_item);
        mClassSpinnerAdapter = new ArrayAdapter<>(mContext, R.layout.yield_spinner_item, mContext.getResources().getStringArray(R.array.classes));
        mClassSpinnerAdapter.setDropDownViewResource(R.layout.yield_spinner_drop_item);

    }

    @Override
    public int getCount() {

        return mProductList.size();
    }

    @Override
    public Object getItem(int position) {
        return mProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) { // 获得当前项的示例
        final ProductInputEntity product;
        product = (ProductInputEntity) getItem(position);
        View view;
        final ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.input_list_item, parent, false);
            viewHolder = new ViewHolder(view, position);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        // 判断编辑按钮是否点击
        if (isEditBtnClicked) {
            viewHolder.deleteImg.setVisibility(View.VISIBLE);
        } else {
            viewHolder.deleteImg.setVisibility(View.GONE);
        }

        viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                // product.isChecked = isChecked;
                if (mOnCheckClickListener != null) {
                    mOnCheckClickListener.onClick(position, isChecked);
                }

            }
        });

        /** 设置spinner下拉框设配器 **/
        viewHolder.reasonsSp.setAdapter(mReasonSpinnerAdapter);
        viewHolder.classesSp.setAdapter(mClassSpinnerAdapter);

        /** 设置spinner的默认值 **/
        for (int i = 0; i < mReasonSpinnerAdapter.getCount(); i++) {
            if (mReasonSpinnerAdapter.getItem(i).toString().equals(product.REASONS)) {
                viewHolder.reasonsSp.setSelection(i);// 默认选中项
                break;
            }
        }
        for (int i = 0; i < mClassSpinnerAdapter.getCount(); i++) {
            if (mClassSpinnerAdapter.getItem(i).toString().equals(product.CLASSES)) {
                viewHolder.classesSp.setSelection(i);// 默认选中项
                break;
            }
        }

        viewHolder.classesSp.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String spValue = (String) viewHolder.classesSp.getSelectedItem();
                if (position == 0) {
                    product.CLASSES = "";
                } else {
                    product.CLASSES = spValue;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        /******************************************************** 分割线 *******************************************************/
        /******************************************** 解决edittext中数据丢失、错乱问题，共5个步骤 ****************************************/

        /** 1、为了避免TextWatcher在第2步被调用，提前将他移除。 **/
        if (viewHolder.machineSpeedEt.getTag() instanceof TextWatcher) {
            viewHolder.machineSpeedEt.removeTextChangedListener((TextWatcher) (viewHolder.machineSpeedEt.getTag()));
        }
        if (viewHolder.rotationRateEt.getTag() instanceof TextWatcher) {
            viewHolder.rotationRateEt.removeTextChangedListener((TextWatcher) (viewHolder.rotationRateEt.getTag()));
        }
        if (viewHolder.jonNumEt.getTag() instanceof TextWatcher) {
            viewHolder.jonNumEt.removeTextChangedListener((TextWatcher) (viewHolder.jonNumEt.getTag()));
        }
        if (viewHolder.takedutyFramesEt.getTag() instanceof TextWatcher) {
            viewHolder.takedutyFramesEt.removeTextChangedListener((TextWatcher) (viewHolder.takedutyFramesEt.getTag()));
        }
        if (viewHolder.onDutyFramesEt.getTag() instanceof TextWatcher) {
            viewHolder.onDutyFramesEt.removeTextChangedListener((TextWatcher) (viewHolder.onDutyFramesEt.getTag()));
        }
        if (viewHolder.changeFramesEt.getTag() instanceof TextWatcher) {
            viewHolder.changeFramesEt.removeTextChangedListener((TextWatcher) (viewHolder.changeFramesEt.getTag()));
        }
        if (viewHolder.stopHoursEt.getTag() instanceof TextWatcher) {
            viewHolder.stopHoursEt.removeTextChangedListener((TextWatcher) (viewHolder.stopHoursEt.getTag()));
        }

        /** 第2步：移除TextWatcher之后，初始化view的内容。 **/
        viewHolder.machineNoTv.setText(product.MACHINENO);
        viewHolder.clothSizeTv.setText(product.CLOTHSIZE);
        viewHolder.productTypeTv.setText(product.PRODUCTTYPE);
        viewHolder.clothNoTv.setText(product.CLOTHNO);
        viewHolder.clothTktNoTv.setText(product.CLOTHTKTNO);
        viewHolder.machineSpeedEt.setText(product.MACHINE_SPEED);

        viewHolder.takedutyFramesEt.setEnabled(true);
        viewHolder.takedutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
        viewHolder.onDutyFramesEt.setEnabled(true);
        viewHolder.onDutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
        viewHolder.changeFramesEt.setEnabled(true);
        viewHolder.changeFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));

        if (!TextUtils.isEmpty(product.TAKEDUTY_FRAMES) || !TextUtils.isEmpty(product.ONDUTY_FRAMES) || !TextUtils.isEmpty(product.CHANGE_FRAMES)) {
            if (TextUtils.isEmpty(product.TAKEDUTY_FRAMES)) {
                viewHolder.takedutyFramesEt.setEnabled(false);
                viewHolder.takedutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_gray));
            }
            if (TextUtils.isEmpty(product.ONDUTY_FRAMES)) {
                viewHolder.onDutyFramesEt.setEnabled(false);
                viewHolder.onDutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_gray));
            }

            if (TextUtils.isEmpty(product.CHANGE_FRAMES)) {
                viewHolder.changeFramesEt.setEnabled(false);
                viewHolder.changeFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_gray));
            }
        }

        viewHolder.clothWeightTv.setText(product.CLOTHWEIGHT);
        viewHolder.requireWeightTv.setText(product.REQUIREWEIGHT);
        viewHolder.rotationRateEt.setText(product.ROTATION_RATE);
        // viewholder.classesSp.setT
        viewHolder.jonNumEt.setText(product.JOB_NUM);
        viewHolder.takedutyFramesEt.setText(product.TAKEDUTY_FRAMES);
        viewHolder.onDutyFramesEt.setText(product.ONDUTY_FRAMES);
        viewHolder.changeFramesEt.setText(product.CHANGE_FRAMES);
        if (TextUtils.isEmpty(product.REQUIREWEIGHT)) {
            viewHolder.requireWeightTv.setText("0.0");
        }

        // 根据product中班次内容的值设置下拉框
        for (int i = 0; i < mClassesSpList.size(); i++) {
            String str = mClassesSpList.get(i);
            if (str.equals(product.CLASSES)) {
                viewHolder.classesSp.setSelection(i, true);
                break;
            } else {
                viewHolder.classesSp.setSelection(0, true);
            }
        }

        /*************************************************/
        // if (viewHolder.reasonsSp.getTag() instanceof OnItemSelectedListener)
        // {
        // viewHolder.reasonsSp.removeOnItemSelectedListener((OnItemSelectedListener)
        // (viewHolder.reasonsSp.getTag()));
        // }

        viewHolder.reasonsSp.setOnItemSelectedListener(null);

        // 根据product中原因内容的值设置下拉框
        for (int i = 0; i < mReasonSpList.size(); i++) {
            String str = mReasonSpList.get(i);
            if (str.equals(product.REASONS)) {
                viewHolder.reasonsSp.setSelection(i, true);
                break;
            } else {
                viewHolder.reasonsSp.setSelection(0, true);
            }
        }

        ReasonSpListener reasonSpListener = new ReasonSpListener(viewHolder, product);
        viewHolder.reasonsSp.setOnItemSelectedListener(reasonSpListener);
        // viewHolder.reasonsSp.setTag(reasonSpListener);

        /**********************************************/

        viewHolder.stopHoursEt.setText(product.STOPHOURS);

        // 设置checkbox状态
        if (product.isChecked == true) {
            viewHolder.checkBox.setChecked(true);
        } else {
            viewHolder.checkBox.setChecked(false);
        }

        /** 3、实例化Textwatcher监听器 **/
        MyTextWatcher machineSpeedWatcher = new MyTextWatcher(viewHolder, viewHolder.machineSpeedEt, position);
        MyTextWatcher rotationRateWatcher = new MyTextWatcher(viewHolder, viewHolder.rotationRateEt, position);
        // MyTextWatcher jonNumWatcher=new MyTextWatcher(viewholder.jonNumEt,
        // position);
        MyTextWatcher takedutyFramesWatcher = new MyTextWatcher(viewHolder, viewHolder.takedutyFramesEt, position);
        MyTextWatcher onDutyFramesWatcher = new MyTextWatcher(viewHolder, viewHolder.onDutyFramesEt, position);
        MyTextWatcher changeFramesWatcher = new MyTextWatcher(viewHolder, viewHolder.changeFramesEt, position);
        MyTextWatcher stopHoursWatcher = new MyTextWatcher(viewHolder, viewHolder.stopHoursEt, position);

        /** 4、设置edittext的监听器 **/
        viewHolder.machineSpeedEt.addTextChangedListener(machineSpeedWatcher);
        viewHolder.rotationRateEt.addTextChangedListener(rotationRateWatcher);
        // viewholder.jonNumEt.addTextChangedListener(jonNumWatcher);
        viewHolder.takedutyFramesEt.addTextChangedListener(takedutyFramesWatcher);
        viewHolder.onDutyFramesEt.addTextChangedListener(onDutyFramesWatcher);
        viewHolder.changeFramesEt.addTextChangedListener(changeFramesWatcher);
        viewHolder.stopHoursEt.addTextChangedListener(stopHoursWatcher);

        /** 5、设置edittext的tag 传入textwatcher **/
        viewHolder.machineSpeedEt.setTag(machineSpeedWatcher);
        viewHolder.rotationRateEt.setTag(rotationRateWatcher);
        // viewholder.jonNumEt.setTag(jonNumWatcher);
        viewHolder.takedutyFramesEt.setTag(takedutyFramesWatcher);
        viewHolder.onDutyFramesEt.setTag(onDutyFramesWatcher);
        viewHolder.changeFramesEt.setTag(changeFramesWatcher);
        viewHolder.stopHoursEt.setTag(stopHoursWatcher);

        /****************************************************** 分割线 ***********************************************************/
        /******************************************* 解决edittext焦点丢失问题 *******************************************************/

        /** 1、设置touch监听器，在点击的时候记录当前点击的位置 **/
        viewHolder.machineSpeedEt.setOnTouchListener(new MyTouchListener(viewHolder.machineSpeedEt, position));
        viewHolder.rotationRateEt.setOnTouchListener(new MyTouchListener(viewHolder.rotationRateEt, position));
        viewHolder.takedutyFramesEt.setOnTouchListener(new MyTouchListener(viewHolder.takedutyFramesEt, position));
        viewHolder.onDutyFramesEt.setOnTouchListener(new MyTouchListener(viewHolder.onDutyFramesEt, position));
        viewHolder.changeFramesEt.setOnTouchListener(new MyTouchListener(viewHolder.changeFramesEt, position));
        viewHolder.stopHoursEt.setOnTouchListener(new MyTouchListener(viewHolder.stopHoursEt, position));

        /** 2、恢复焦点 **/
        if (mIndex != -1) {
            // 在点击了数据的情况下，执行下面逻辑
            if (mIndex == position) {
                // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
                // 获取了焦点，但是光标不闪，重新调用setText方法即可
                // 2017年8月7日 新增功能 ： 接班格数、交班格数、当班格数
                // 三个格数不能同时录入数据，当录入了一个后，其它两个禁止录入。
                final int id = mEditText.getId();
                if (id == R.id.input_item_MachineSpeedEt) {
                    viewHolder.machineSpeedEt.requestFocus();
                    viewHolder.machineSpeedEt.setText(viewHolder.machineSpeedEt.getText().toString());
                    viewHolder.machineSpeedEt.setSelection(viewHolder.machineSpeedEt.getText().length());
                } else if (id == R.id.input_item_RotationRateEt) {
                    viewHolder.rotationRateEt.requestFocus();
                    viewHolder.rotationRateEt.setText(viewHolder.rotationRateEt.getText().toString());
                    viewHolder.rotationRateEt.setSelection(viewHolder.rotationRateEt.getText().length());
                } else if (id == R.id.input_item_takedutyFramesEt) {
                    viewHolder.takedutyFramesEt.requestFocus();
                    viewHolder.takedutyFramesEt.setText(viewHolder.takedutyFramesEt.getText().toString());
                    viewHolder.takedutyFramesEt.setSelection(viewHolder.takedutyFramesEt.getText().length());
                } else if (id == R.id.input_item_ondutyFramesEt) {
                    viewHolder.onDutyFramesEt.requestFocus();
                    viewHolder.onDutyFramesEt.setText(viewHolder.onDutyFramesEt.getText().toString());
                    viewHolder.onDutyFramesEt.setSelection(viewHolder.onDutyFramesEt.getText().length());
                } else if (id == R.id.input_item_changeFramesEt) {
                    viewHolder.changeFramesEt.requestFocus();
                    viewHolder.changeFramesEt.setText(viewHolder.changeFramesEt.getText().toString());
                    viewHolder.changeFramesEt.setSelection(viewHolder.changeFramesEt.getText().length());
                } else if (id == R.id.input_item_stopHoursEt) {
                    viewHolder.stopHoursEt.requestFocus();
                    viewHolder.stopHoursEt.setText(viewHolder.stopHoursEt.getText().toString());
                    viewHolder.stopHoursEt.setSelection(viewHolder.stopHoursEt.getText().length());
                }
            } else {
                viewHolder.machineSpeedEt.clearFocus();
                viewHolder.rotationRateEt.clearFocus();
                viewHolder.jonNumEt.clearFocus();
                viewHolder.takedutyFramesEt.clearFocus();
                viewHolder.onDutyFramesEt.clearFocus();
                viewHolder.changeFramesEt.clearFocus();
                viewHolder.stopHoursEt.clearFocus();
            }
        }

        /****************************************************** 分割线 ***********************************************************/

        viewHolder.deleteImg.setOnClickListener(v -> {
            if (mOnDeleteClickListener != null) {
                mOnDeleteClickListener.onClick(position);
            }

        });

        return view;

    }

    private onDeleteClickListener mOnDeleteClickListener;

    /**
     * 设置删除接口
     */
    public void setOnDeleteClickListener(onDeleteClickListener onDeleteClickListener) {
        mOnDeleteClickListener = onDeleteClickListener;
    }

    /**
     * 删除接口
     *
     * @author zhu_yu
     */
    public interface onDeleteClickListener {
        public void onClick(int position);
    }

    private onCheckClickListener mOnCheckClickListener;

    /**
     * 设置选择框接口
     */
    public void setOnCheckClickListener(onCheckClickListener onCheckClickListener) {
        mOnCheckClickListener = onCheckClickListener;
    }

    /**
     * 选择框接口
     *
     * @author zhu_yu
     */
    public interface onCheckClickListener {
        public void onClick(int position, boolean isChecked);
    }

    private OnEditTextClickListener onEditTextClickListener;

    public void setOnEditTextClickListener(OnEditTextClickListener listener) {
        onEditTextClickListener = listener;
    }

    /**
     * 当输入框被点击
     */
    public interface OnEditTextClickListener {
        void onClick(Bundle bundle);
    }

    private void setEditTextBundle(TextView et, String name, int position) {
        et.setInputType(InputType.TYPE_NULL);
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        bundle.putString("name", name);
        et.setTag(R.id.inputEditTag, bundle);
        et.setOnClickListener(editOnClick);
    }

    public void updateEditText(Bundle extras, String value) {
        if (extras != null) {
            int pos = extras.getInt("position");
            if (mProductList.size() > pos) {
                ProductInputEntity item = mProductList.get(pos);
                String name = extras.getString("name");
                item.latestChangedInput = "";
                if (!TextUtils.isEmpty(name)) {
                    switch (name) {
                        case "machineSpeed": {
                            item.MACHINE_SPEED = value;
                            notifyDataSetChanged();
                            break;
                        }
                        case "rotationRate": {
                            item.ROTATION_RATE = value;
                            notifyDataSetChanged();
                            break;
                        }
                        case "takedutyFrames": {
                            item.TAKEDUTY_FRAMES = value;
                            item.latestChangedInput = "takedutyFrames";
                            notifyDataSetChanged();
                            break;
                        }
                        case "onDutyFrames": {
                            item.ONDUTY_FRAMES = value;
                            item.latestChangedInput = "onDutyFrames";
                            notifyDataSetChanged();
                            break;
                        }
                        case "changeFrames": {
                            item.CHANGE_FRAMES = value;
                            item.latestChangedInput = "changeFrames";
                            notifyDataSetChanged();
                            break;
                        }
                        case "stopHours": {
                            item.STOPHOURS = value;
                            item.latestChangedInput = "stopHours";
                            // 更改所有该车号的停台小时
                            for (int i = 0; i < mProductList.size(); i++) {
                                if (mProductList.get(pos).MACHINENO.equals(mProductList.get(i).MACHINENO)) {
                                    mProductList.get(i).STOPHOURS = mProductList.get(pos).STOPHOURS;
                                    mProductList.get(i).isChecked = true;
                                    notifyDataSetChanged(mListView, i);
                                }
                            }
                            notifyDataSetChanged();
                            break;
                        }
                    }
                }
            }
        }
    }

    private View.OnClickListener editOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v instanceof TextView) {
                TextView editText = (TextView) v;
                Bundle bundle = (Bundle) editText.getTag(R.id.inputEditTag);
                if (bundle != null) {
                    int position = bundle.getInt("position");
                    String name = bundle.getString("name");

                    if (onEditTextClickListener != null) {
                        Bundle extras = new Bundle();
                        extras.putInt("position", position);
                        extras.putString("name", name);
                        extras.putInt(SimpleInputDialog.INPUT_TYPE, InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                        extras.putString("title", "请输入:");
                        extras.putString("value", editText.getText().toString());
                        onEditTextClickListener.onClick(extras);
                    }
                }
            }
        }
    };

    private class MyTouchListener implements OnTouchListener {
        private int position;
        private EditText editText;

        // private ViewHolder viewHolder;
        public MyTouchListener(EditText editText, int position) {
            this.editText = editText;
            this.position = position;
            // viewHolder=viewHolder;
            //
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
//            if (event.getAction() == MotionEvent.ACTION_UP) {
            mIndex = position;
            mEditText = editText;
            editText.postDelayed(editText::requestFocus, 1500);
//            editText.requestFocus();
//            editText.setText(editText.getText().toString());
//            }
            return false;
        }
    }

    private class EditClickListener implements View.OnClickListener {
        private final int position;

        public EditClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (v instanceof EditText) {
                mEditText = (EditText) v;
                ToastUtil.longToast(mEditText.getContext(), mEditText.getTag(R.id.tag_1).toString());
                mIndex = position;
                mEditText.requestFocus();
            }
        }
    }

    /**
     * 原因选择监听器
     *
     * @author zhu_yu
     */
    private class ReasonSpListener implements OnItemSelectedListener {

        private ProductInputEntity product;
        private ViewHolder viewHolder;

        public ReasonSpListener(ViewHolder viewHolder, ProductInputEntity product) {

            this.product = product;
            this.viewHolder = viewHolder;
        }

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (!isFirst) {
                String spValue = (String) viewHolder.reasonsSp.getSelectedItem();
                if (position == 0) {
                    product.REASONS = "";
                } else {
                    product.REASONS = spValue;
                }
                // 同步更改所有该车号的原因,同时把勾选框勾上
                for (int i = 0; i < mProductList.size(); i++) {
                    if (product.MACHINENO.equals(mProductList.get(i).MACHINENO)) {

                        mProductList.get(i).REASONS = product.REASONS;
                        mProductList.get(i).isChecked = true;
                        notifyDataSetChanged(mListView, i);
                    }
                }

            } else {
                isFirst = false;
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }

    }

    ;

    /**
     * 文本变化监听器.随时保存用户输入的值到list中
     *
     * @author zhu_yu
     * @Description<功能详细描述>
     * @Version [版本号, 2017-6-6]
     */
    private class MyTextWatcher implements TextWatcher {

        private TextView editText;
        private int position;
        private ViewHolder viewHolder;
        private String stopHours = "";

        public MyTextWatcher(ViewHolder viewHolder, TextView editText, int position) {
            this.editText = editText;
            this.position = position;
            this.viewHolder = viewHolder;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (editText.getId() == R.id.input_item_stopHoursEt) {
                stopHours = s.toString();
            }

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            final int id = editText.getId();
            if (id == R.id.input_item_MachineSpeedEt) {
                mProductList.get(position).MACHINE_SPEED = s.toString();
            } else if (id == R.id.input_item_RotationRateEt) {
                mProductList.get(position).ROTATION_RATE = s.toString();
            } else if (id == R.id.input_item_jobNumTv) {
                mProductList.get(position).JOB_NUM = s.toString();
            } else if (id == R.id.input_item_takedutyFramesEt) {
                mProductList.get(position).TAKEDUTY_FRAMES = s.toString();
                if (!TextUtils.isEmpty(s)) {
                    viewHolder.takedutyFramesEt.setEnabled(true);
                    viewHolder.onDutyFramesEt.setEnabled(false);
                    viewHolder.changeFramesEt.setEnabled(false);
                    viewHolder.takedutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                    viewHolder.onDutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_gray));
                    viewHolder.changeFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_gray));
                } else {
                    viewHolder.takedutyFramesEt.setEnabled(true);
                    viewHolder.onDutyFramesEt.setEnabled(true);
                    viewHolder.changeFramesEt.setEnabled(true);
                    viewHolder.takedutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                    viewHolder.onDutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                    viewHolder.changeFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                }
                checkTakeDutyFrames(viewHolder, position);
            } else if (id == R.id.input_item_ondutyFramesEt) {
                mProductList.get(position).ONDUTY_FRAMES = s.toString();
                if (!TextUtils.isEmpty(s)) {
                    viewHolder.onDutyFramesEt.setEnabled(true);
                    viewHolder.takedutyFramesEt.setEnabled(false);
                    viewHolder.changeFramesEt.setEnabled(false);
                    viewHolder.onDutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                    viewHolder.takedutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_gray));
                    viewHolder.changeFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_gray));
                } else {
                    viewHolder.takedutyFramesEt.setEnabled(true);
                    viewHolder.changeFramesEt.setEnabled(true);
                    viewHolder.onDutyFramesEt.setEnabled(true);
                    viewHolder.takedutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                    viewHolder.onDutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                    viewHolder.changeFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                }

                checkOnDutyFrames(viewHolder, position);
            } else if (id == R.id.input_item_changeFramesEt) {
                mProductList.get(position).CHANGE_FRAMES = s.toString();
                if (!TextUtils.isEmpty(s)) {
                    viewHolder.changeFramesEt.setEnabled(true);
                    viewHolder.onDutyFramesEt.setEnabled(false);
                    viewHolder.takedutyFramesEt.setEnabled(false);
                    viewHolder.changeFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                    viewHolder.onDutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_gray));
                    viewHolder.takedutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_gray));
                } else {
                    viewHolder.takedutyFramesEt.setEnabled(true);
                    viewHolder.changeFramesEt.setEnabled(true);
                    viewHolder.onDutyFramesEt.setEnabled(true);
                    viewHolder.takedutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                    viewHolder.onDutyFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                    viewHolder.changeFramesEt.setBackground(mContext.getResources().getDrawable(R.drawable.bg_edittext_white));
                }
                checkChangeFrames(viewHolder, position);
            } else if (id == R.id.input_item_stopHoursEt) {

                if (stopHours.equals(s.toString())) {
                    return;
                }
                mProductList.get(position).STOPHOURS = s.toString();

                // 更改所有该车号的停台小时
                for (int i = 0; i < mProductList.size(); i++) {
                    if (mProductList.get(position).MACHINENO.equals(mProductList.get(i).MACHINENO)) {
                        mProductList.get(i).STOPHOURS = mProductList.get(position).STOPHOURS;
                        mProductList.get(i).isChecked = true;
                        notifyDataSetChanged(mListView, i);
                    }
                }
                // InputListAdapter.this.notifyDataSetChanged();
                checkStopHours(viewHolder, position);
            }

        }
    }

    /**
     * 校验停台小时
     *
     * @param viewHolder
     */
    private void checkStopHours(final ViewHolder viewHolder, int position) {
        ProductInputEntity product = (ProductInputEntity) getItem(position);

        double stopHours = 0.0;
        if (!TextUtils.isEmpty(viewHolder.stopHoursEt.getText().toString())) {
            stopHours = Double.parseDouble(viewHolder.stopHoursEt.getText().toString());

        }
        if (stopHours > 10) {
            String msg = "停台小时最大为10小时，请重新输入！";

            SimpleDialog.show(mContext, msg, (SimpleDialog.OnPositiveClickListener) () -> viewHolder.stopHoursEt.setText(""));

        }

    }

    /**
     * 校验当班格数
     *
     * @param viewHolder
     */
    private void checkOnDutyFrames(final ViewHolder viewHolder, int position) {
        ProductInputEntity product = (ProductInputEntity) getItem(position);

        // 获取用户输入的当班格数
        double onDutyFrames = 0.0;
        if (!TextUtils.isEmpty(viewHolder.onDutyFramesEt.getText().toString())) {
            try {
                onDutyFrames = Double.parseDouble(viewHolder.onDutyFramesEt.getText().toString());
            } catch (NumberFormatException e) {
                SimpleDialog.show(mContext, "请输入正确的格数!", (SimpleDialog.OnPositiveClickListener) () -> {
                });
            }
        }
        // 要求重量=格数*2.5
        double requireWeight = onDutyFrames * 2.5;
        product.REQUIREWEIGHT = requireWeight + "";
        viewHolder.requireWeightTv.setText(requireWeight + "");

        if (Double.parseDouble(product.RM_REQUIREWEIGHT) <= 0) {
            String msg = "最大要求重量为0或负数！";
            SimpleDialog.show(mContext, msg, (SimpleDialog.OnPositiveClickListener) () -> {

            });
            return;
        }

        if (requireWeight > Double.parseDouble(product.RM_REQUIREWEIGHT)) {
            // 字体颜色设为红色
            viewHolder.requireWeightTv.setTextColor(mContext.getResources().getColor(R.color.red));

            String msg = "当班格数不能大于" + Double.parseDouble(product.RM_REQUIREWEIGHT) / 2.5 + ",请重新输入！";

            SimpleDialog.show(mContext, msg, (SimpleDialog.OnPositiveClickListener) () -> viewHolder.onDutyFramesEt.setText(""));

        } else {
            viewHolder.requireWeightTv.setTextColor(mContext.getResources().getColor(R.color.black));
        }
    }

    /**
     * 校验接班格数
     *
     * @param viewHolder
     * @param position
     */
    private void checkTakeDutyFrames(final ViewHolder viewHolder, int position) {
        ProductInputEntity product = (ProductInputEntity) getItem(position);

        // 获取用户输入的当班格数
        double onDutyFrames = 0.0;
        if (!TextUtils.isEmpty(viewHolder.takedutyFramesEt.getText().toString())) {
            try {
                onDutyFrames = Double.parseDouble(viewHolder.takedutyFramesEt.getText().toString());
            } catch (NumberFormatException e) {
                SimpleDialog.show(mContext, "请输入正确的格数!", (SimpleDialog.OnPositiveClickListener) () -> {
                    // viewHolder.changeFramesEt.setText("");
                });
            }
        }
        // 要求重量=格数*2.5
        double requireWeight = onDutyFrames * 2.5;
        product.REQUIREWEIGHT = requireWeight + "";
        viewHolder.requireWeightTv.setText(requireWeight + "");

        if (Double.parseDouble(product.RM_REQUIREWEIGHT) <= 0) {
            String msg = "最大要求重量为0或负数！";
            SimpleDialog.show(mContext, msg, (SimpleDialog.OnPositiveClickListener) () -> {

            });
            return;
        }

        if (requireWeight > Double.parseDouble(product.RM_REQUIREWEIGHT)) {
            viewHolder.requireWeightTv.setTextColor(mContext.getResources().getColor(R.color.red));
            String msg = "接班格数不能大于" + Double.parseDouble(product.RM_REQUIREWEIGHT) / 2.5 + ",请重新输入！";
            SimpleDialog.show(mContext, msg, (SimpleDialog.OnPositiveClickListener) () -> viewHolder.takedutyFramesEt.setText(""));

        } else {
            viewHolder.requireWeightTv.setTextColor(mContext.getResources().getColor(R.color.black));
        }
    }

    /**
     * 校验交班格数
     *
     * @param viewHolder
     * @param position
     */
    private void checkChangeFrames(final ViewHolder viewHolder, int position) {
        ProductInputEntity product = (ProductInputEntity) getItem(position);

        // 获取用户输入的当班格数
        double onDutyFrames = 0.0;
        if (!TextUtils.isEmpty(viewHolder.changeFramesEt.getText().toString())) {
            try {
                onDutyFrames = Double.parseDouble(viewHolder.changeFramesEt.getText().toString());
            } catch (NumberFormatException e) {
                SimpleDialog.show(mContext, "请输入正确的格数！", (SimpleDialog.OnPositiveClickListener) () -> {
                });
            }

        }
        // 要求重量=格数*2.5
        double requireWeight = onDutyFrames * 2.5;
        product.REQUIREWEIGHT = requireWeight + "";
        viewHolder.requireWeightTv.setText(requireWeight + "");

        if (Double.parseDouble(product.RM_REQUIREWEIGHT) <= 0) {
            String msg = "最大要求重量为0或负数！";
            SimpleDialog.show(mContext, msg, (SimpleDialog.OnPositiveClickListener) () -> {
            });
            return;
        }

        if (requireWeight > Double.parseDouble(product.RM_REQUIREWEIGHT)) {
            viewHolder.requireWeightTv.setTextColor(mContext.getResources().getColor(R.color.red));
            String msg = "交班格数不能大于" + Double.parseDouble(product.RM_REQUIREWEIGHT) / 2.5 + ",请重新输入！";
            SimpleDialog.show(mContext, msg, (SimpleDialog.OnPositiveClickListener) () -> viewHolder.changeFramesEt.setText(""));

        } else {
            viewHolder.requireWeightTv.setTextColor(mContext.getResources().getColor(R.color.black));
        }
    }

    /**
     * 设置删除按钮状态
     *
     * @param isClicked
     */
    public void setEditBtnStatu(boolean isClicked) {
        isEditBtnClicked = isClicked;
        this.notifyDataSetChanged();
    }

    // 设置下拉框文本值
    public void setSpList(List<String> spList) {
        mReasonSpList.clear();
        mReasonSpList.addAll(spList);
        mReasonSpinnerAdapter.notifyDataSetChanged();
    }

    /**
     * 设置配置适配器的listview 如果需要在adapter中局部刷新数据，需要传入listview
     *
     * @param listView
     */
    public void setListView(ListView listView) {
        mListView = listView;
    }

    /**
     * 设置光标位置
     *
     * @param index
     */
    public void setFocusIndex(int index) {
        mIndex = index;
    }

    /**
     * 局部更新数据，调用一次getView()方法；Google推荐的做法
     *
     * @param listView 要更新的listview
     * @param position 要更新的位置
     */
    public void notifyDataSetChanged(ListView listView, int position) {
        if (listView == null) {
            return;
        }
        /** 第一个可见的位置 **/
        int firstVisiblePosition = listView.getFirstVisiblePosition();
        /** 最后一个可见的位置 **/
        int lastVisiblePosition = listView.getLastVisiblePosition();

        /** 在看见范围内才更新，不可见的滑动后自动会调用getView方法更新 **/
        if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
            /** 获取指定位置view对象 **/
            View view = listView.getChildAt(position - firstVisiblePosition);
            getView(position, view, listView);
        }
    }

    /**
     * 局部刷新，多项数据
     *
     * @param listView
     * @param positionList
     */
    public void notifyDataSetChanged(ListView listView, List<Integer> positionList) {
        for (int position : positionList) {
            notifyDataSetChanged(listView, position);
        }
    }

    /**
     * 对控件实例进行缓存
     */
    class ViewHolder {
        int position;

        /**
         * 车号
         **/
        TextView machineNoTv;
        /**
         * 尺寸
         **/
        TextView clothSizeTv;
        /**
         * 胚布类型
         **/
        TextView productTypeTv;
        /**
         * 布号
         **/
        TextView clothNoTv;
        /**
         * 三联单号
         **/
        TextView clothTktNoTv;
        /**
         * 车速
         **/
        EditText machineSpeedEt;
        /**
         * 转速
         **/
        EditText rotationRateEt;
        /**
         * 班次
         **/
        Spinner classesSp;
        /**
         * 工号
         **/
        TextView jonNumEt;
        /**
         * 接班格数
         **/
        EditText takedutyFramesEt;
        /**
         * 当班格数
         **/
        EditText onDutyFramesEt;
        /**
         * 交班格数
         **/
        EditText changeFramesEt;
        /**
         * 布票重量
         **/
        TextView clothWeightTv;
        /**
         * 要求重量
         **/
        TextView requireWeightTv;
        /**
         * 其他原因
         **/
        Spinner reasonsSp;
        /**
         * 停台小时
         **/
        EditText stopHoursEt;
        /**
         * 删除
         **/
        Button deleteImg;
        /**
         * 是否选中
         **/
        CheckBox checkBox;

        public ViewHolder(View view, final int position) {
            machineNoTv = view.findViewById(R.id.input_item_machineNoTv);
            clothSizeTv = view.findViewById(R.id.input_item_clothSizeTv);
            productTypeTv = view.findViewById(R.id.input_item_productTypeTv);
            clothNoTv = view.findViewById(R.id.input_item_clothNoTv);
            clothTktNoTv = view.findViewById(R.id.input_item_clothTktNoTv);
            machineSpeedEt = view.findViewById(R.id.input_item_MachineSpeedEt);
            rotationRateEt = view.findViewById(R.id.input_item_RotationRateEt);
            classesSp = view.findViewById(R.id.input_item_classesSp);
            jonNumEt = view.findViewById(R.id.input_item_jobNumTv);
            takedutyFramesEt = view.findViewById(R.id.input_item_takedutyFramesEt);
            onDutyFramesEt = view.findViewById(R.id.input_item_ondutyFramesEt);
            changeFramesEt = view.findViewById(R.id.input_item_changeFramesEt);
            clothWeightTv = view.findViewById(R.id.input_item_clothWeightTv);
            requireWeightTv = view.findViewById(R.id.input_item_requireWeightTv);
            reasonsSp = view.findViewById(R.id.input_item_reasonsSp);
            stopHoursEt = view.findViewById(R.id.input_item_stopHoursEt);
            deleteImg = view.findViewById(R.id.input_item_deleteBtn);
            checkBox = view.findViewById(R.id.input_item_checkBox);

        }

    }

}
